﻿using System;
using System.Globalization;
using System.Linq;

namespace ContractManager.Common.Service
{
    /// <summary>
    /// Text to number service. 
    /// </summary>
    /// <remarks>
    /// Golovkov Alex, 17.12.2010. 
    /// </remarks>
    public static class TextToNumberService
    {
        #region Constants and Fields

        /// <summary> true if is m </summary>
        private static bool isM;

        /// <summary> The replace text value </summary>
        private static decimal replaceTextValue;

        #endregion

        #region Properties

        /// <summary>   Gets the replace text value. </summary>
        /// <value> The replace text value. </value>
        public static decimal ReplaceTextValue
        {
            get
            {
                if (isM)
                {
                    replaceTextValue = replaceTextValue * 1000000;
                }

                return replaceTextValue;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets a neg value. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 17.12.2010. 
        /// </remarks>
        /// <param name="value">
        /// The value. 
        /// </param>
        /// <returns>
        /// The neg value. 
        /// </returns>
        public static decimal GetNegValue(decimal value)
        {
            return value > 0 ? value * -1 : value;
        }

        /// <summary>
        /// Replace tex rate. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 17.12.2010. 
        /// </remarks>
        /// <param name="defaultValue">
        /// Default value. 
        /// </param>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <returns>
        /// . 
        /// </returns>
        public static string ReplaceTexRate(double defaultValue, string text)
        {
            text = text.Replace('.', ',');
            return text.Contains(',') ? text : String.Format("{0},{1}", defaultValue, text);
        }

        /// <summary>
        /// Replace tex value. 
        /// </summary>
        /// <remarks>
        /// Golovkov Alex, 17.12.2010. 
        /// </remarks>
        /// <param name="s">
        /// The string. 
        /// </param>
        /// <returns>
        /// decimal value 
        /// </returns>
        public static decimal ReplaceTexValue(string s)
        {
            // s = s.Replace('.', ',');
            if (s.Contains("m"))
            {
                s = s.Substring(0, s.Length - 1);
                isM = true;
            }
            else
            {
                isM = false;
            }

            if (Decimal.TryParse(s, NumberStyles.Currency, CultureInfo.CurrentCulture, out replaceTextValue))
            {
                return ReplaceTextValue;
            }

            if (Decimal.TryParse(s, NumberStyles.Currency, CultureInfo.InvariantCulture, out replaceTextValue))
            {
                return ReplaceTextValue;
            }

            return ReplaceTextValue;
        }

        #endregion
    }
}
